#ifndef SOCKET_BUFFER_H
#define SOCKET_BUFFER_H

#include "BlockPool.hpp"

#include <cstddef>
#include <mutex>
#include <utility>
#include <vector>
#include <unordered_map>
#include <queue>
#include <memory>

namespace DBServer 
{
namespace Socket 
{

typedef struct Request {

    Request(char *request, short len_) 
    : s(request), len(len_)
    {}

    ~Request() {
        if (s != nullptr)
            delete[] s;      
    }
    // replace std::string for c char*
    char *s;
    short len;
} Request;

class TcpBuffer {
public:
    TcpBuffer();
    ~TcpBuffer();

    std::vector<std::shared_ptr<Request>> read(int, int&);
    void write(std::string& respond);
    int push(int);

private:
    std::vector<std::pair<char*, short>> analize(int, int&);
private:

    // write
    // mapping the respond uuid with it's ti
    using consume_queue_t = std::queue<std::pair<MemoryBlock *, size_t>>;
    consume_queue_t             consume_queue;
    std::mutex                  write_mtx;
    std::mutex                  map_mtx;

    // read
    short                       data_len;
    int                         cur_len_head;
    int                         cur_len_data;
    char                        *data;
    int                         status;
    MemoryBlock                 *buffer;
};

} // DBServer
} // Socket

#endif //SOCKET_BUFFER_H
